package cn.wyf.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

import cn.wyf.demo.model.Course;
import cn.wyf.demo.service.CourseService;

@Controller
@RequestMapping("/course")
public class CourseController {
    private CourseService courseService;

    @Autowired
    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public ResponseEntity<Course> getCoursebyId(@RequestParam("courseId") Integer id) {
        Course course = courseService.getCoursebyId(id);
        return new ResponseEntity<Course>(course, HttpStatus.OK);
    }

    @RequestMapping(value = "/test1", method = RequestMethod.POST)
    public @ResponseBody
    Course getCoursebyId1(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("courseId");
        String coursename = request.getParameter("courseName");
        System.out.println(coursename + "===course test1=====" + id);
        return courseService.getCoursebyId(Integer.valueOf(id));
    }
}
